package com.wrx.server.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wrx.server.controller.UserController;
import com.wrx.server.mapper.BarMapper;
import com.wrx.server.mapper.CollectMapper;
import com.wrx.server.mapper.ThemePostMapper;
import com.wrx.server.pojo.*;
import com.wrx.server.service.IBarService;
import com.wrx.server.service.ICollectService;
import com.wrx.server.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wrx
 * @since 2021-04-20
 */
@Service
public class BarServiceImpl extends ServiceImpl<BarMapper, Bar> implements IBarService {

    @Autowired
    private BarMapper barMapper;
    @Autowired
    private ThemePostMapper themePostMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    ICollectService collectService;


    /**
     * 创建吧
     *
     * @param bar
     * @param request
     * @return
     */
    @Override
    public RespBean registerBar(Bar bar, HttpServletRequest request) {

        //检测吧名是否存在
        if (barMapper.selBarInfoByBarName(bar.getBar_name()) != 0) {
            return RespBean.error("该吧名已存在，请重试");
        }
        if (StringUtils.isBlank(bar.getBar_name())){
            return RespBean.error("创建失败，贴吧名为空");
        }

        //吧logo路径若为空则添加默认Logo
        if (bar.getBar_logo().length()<1|| StringUtils.isBlank(bar.getBar_logo())) {
            bar.setBar_logo("./image/bar/tiebalogo.jpg");
        }
        //吧简介路径若为空则添加默认简介
        if (StringUtils.isBlank(bar.getBar_introduce())) {
            bar.setBar_introduce("这个吧主很懒，什么都没写呢");
        }

        //判断用户登录是否登录，若为登录状态则将当前登录用户的id作为该吧所有人的id
        Integer barOwnerId = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        if (barOwnerId != null) {
            bar.setBar_owner_id(barOwnerId);
            barMapper.insertBar(bar);
            CollectBar collectBar = new CollectBar();
            collectBar.setBar_id(bar.getBar_id());
            collectService.addCollectBar(collectBar,request);
            return RespBean.success("创建" + bar.getBar_name() + "吧成功",bar.getBar_id());
        }


        return RespBean.success("创建吧异常，请重试");
    }



    @Override
    public RespBean putAllUserByPage(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Bar> lists = barMapper.selAllBar();
        PageInfo<Bar> pageInfo = new PageInfo<Bar>(lists);
        return RespBean.success("success",pageInfo);
    }

    @Override
    public RespBean selBarById(int bar_id) {
        if (bar_id<1){
            return RespBean.error("贴吧id异常，请重试");
        }

        List<Bar> list =barMapper.selBarById(bar_id);
        if (list==null){
            return RespBean.error("该贴吧不存在！");
        }else {
            return RespBean.success(list);
        }
    }

    @Override
    public RespBean selBarInfoAndUserNetNameById(int bar_id) {
        if (bar_id<1){
            return RespBean.error("贴吧id异常，请重试！");
        }

        List<BarModel> list = barMapper.selBarInfoAndUserNetNameById(bar_id);
        if (null==list){
            return RespBean.error("该贴吧不存在！");
        }else {
            return RespBean.success(list);
        }
    }

    @Override
    public RespBean selBarInfoUserNetNameCollectStateById(int bar_id,HttpServletRequest request) {
        if (bar_id<=0){
            return RespBean.error("该贴吧不存在");
        }
        List<BarModel> list = barMapper.selBarInfoAndUserNetNameById(bar_id);
        if (null==list){
            return RespBean.error("该贴吧不存在！");
        }

        Integer userId = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        if (userId == null){
            return RespBean.error("关注失败，请先登录！");
        }

        CollectBar collectBar = new CollectBar();
        collectBar.setUser_id(userId);
        collectBar.setBar_id(bar_id);

        Integer collectId = collectMapper.selCollectBarId(collectBar);
        CollectVerifyModel collectVerifyModel = new CollectVerifyModel();
        collectVerifyModel.setObject(list);
        if (null==collectId){
            //该吧未关注
            collectVerifyModel.setCollect_state(false);
            return RespBean.success(collectVerifyModel);
        }else {
            //该把已关注
            collectVerifyModel.setCollect_state(true);
            return RespBean.success(collectVerifyModel);
        }

    }

    @Override
    public RespBean selBarByThemePostId(int theme_post_id) {
        if (theme_post_id<1){
            return RespBean.error("主题帖id异常，请重试!");
        }

        Integer bar_id =themePostMapper.selBarIdByThemePost(theme_post_id);

        if (null ==bar_id) {
            return RespBean.error("主题帖异常，请重试!");
        }
        List<Bar> list = barMapper.selBarById(bar_id);
        return RespBean.success(list);
    }

    @Override
    public RespBean selLikeBarByInfo(String searchInfo) {
        List<Bar> lists;
        if (searchInfo.length()>0) {
           lists = barMapper.selLikeBarByInfo(searchInfo);
           return RespBean.success(lists);
        }
        return null;
    }
}
